package com.tlx.pdf;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;

import com.tlx.core.util.TConfig;
import com.tlx.core.util.TFileUtils;
import com.tlx.util.CmdUtils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;

public class Html2PdfUtils {

    public static final String CMD = TConfig.getString("html2pdf.cmd");

    public static void html2Pdf(String htmlPath) {
        String parent = new File(htmlPath).getParent();
        String pdfPath = new File(parent, FilenameUtils.getBaseName(htmlPath) + ".pdf").getAbsolutePath();
        html2Pdf(htmlPath, pdfPath);
    }

    public static void html2Pdf(String htmlPath, String pdfPath) {
        String cmd = StrUtil.indexedFormat(CMD, htmlPath, pdfPath);
        Console.log("pdf生成完毕：" + pdfPath);
        CmdUtils.exec(cmd);
    }

    public static void html2PdfByDir(String dir, boolean isIncludeSon) {
        List<String> htmlList = TFileUtils.getFileListByExt(dir, "html");

        if (CollectionUtils.isEmpty(htmlList)) {
            return;
        }

        for (String htmlPath : htmlList) {
            html2Pdf(htmlPath);
        }

        // 再递归
        if (isIncludeSon) {
            List<String> sonDirList = TFileUtils.getDirList(dir);
            for (String sonPath : sonDirList) {
                html2PdfByDir(sonPath, isIncludeSon);
            }
        }

    }

}
